﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class PictureList : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String basepath = Server.MapPath("~/") + @"portail\modules\Coppermine\albums\userpics";
            String TargetUrl = String.Empty;
            String TargetThumbUrl = String.Empty;

            if (!Directory.Exists(basepath))
                return;

            IEnumerable<String> pictures = Directory.EnumerateFiles(basepath, "*.*", SearchOption.AllDirectories);
            //IEnumerable<String> pictures = Directory.GetFiles(basepath, "*.*", SearchOption.AllDirectories);

            ImageLink img;

            try
            {
                if (pictures != null)
                    foreach (String pic in pictures)
                    {
                        TargetUrl = HttpUtility.UrlPathEncode(pic.Replace(basepath + "\\",
                            "http://www.teamfrancetriple.com/portail/modules/coppermine/albums/userpics/")
                            .Replace("\\", "/"));

                        if (!TargetUrl.Contains("thumb_"))
                            TargetThumbUrl = TargetUrl.Substring(0, TargetUrl.LastIndexOf("/") + 1) + "thumb_" + TargetUrl.Substring(TargetUrl.LastIndexOf("/") + 1);
                        else
                            TargetThumbUrl = TargetUrl;

                        if (TargetUrl != null && !TargetUrl.Contains("index.html") && !TargetUrl.Contains("thumb_"))
                        {
                            img = new ImageLink();
                            img.ImageUrl = TargetThumbUrl;
                            img.NavigateUrl = TargetUrl;
                            img.AlternateText = Path.GetFileName(pic);
                            //System.Diagnostics.Debug.WriteLine("");
                            PicturesPlaceHolder.Controls.Add(img);
                            System.Diagnostics.Debug.WriteLine(TargetUrl);
                        }
                    }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Message : " + ex.Message + "<br />" + HttpUtility.UrlPathEncode(TargetUrl));
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("RetrieveError".ToLocalizedString(), "PictureList", ex.Message), ex) { EntityType = typeof(PictureList) };

            }
        }

    }
}